<%@ page contentType="text/xml;charset=utf-8"
	import="java.sql.Connection, java.sql.PreparedStatement, java.sql.ResultSet
	, git.bankview.sl.db.DBConnectionFactory
	, git.bankview.wl.auth.userGroups.TransCheck
	, git.bankview.wl.bean.TaskSchedulerBean
	, git.bankview.sl.schedule.TaskScheduler"
%><%@ include file="/jsp/inc/pageUtils.jsp"
%><% //request.setCharacterEncoding("utf-8");
	response.addHeader("Cache-Control", "no-cache"); %>
<jsp:useBean id="oLogon" class="git.bankview.wl.bean.LogonBean" scope="session" />
<handleTaskUpdate>
<%
	String msg = "检查用户权限";
	try
	{
		//TransCheck.check(oLogon.getUsername(), "90060", 255);
		
		msg = "获得任务ID参数";
		int taskid = Integer.parseInt(request.getParameter("taskid"));
		msg = "获得任务日期";
		java.sql.Date datekey = sqlDate(request.getParameter("datekey"));

		msg = "获取计划任务调度实例";
		TaskScheduler ts = TaskScheduler.getInstance();
		msg = "检查计划任务调度实例状态";
		int tsStatus = ts.getStatus();
		if (null != ts.getSchdDate() && datekey.compareTo(ts.getSchdDate()) <= 0 && TaskScheduler.NEXTSCHD != tsStatus)
			throw new java.lang.IllegalStateException("当前任务日期的计划任务实例正在运行(" + tsStatus + ":" + ts.getStatusName(tsStatus) + ")");
			
		String execurl = new String(request.getParameter("execurl").getBytes("iso8859_1"), "utf-8");
		int retrymax = Integer.parseInt(request.getParameter("retrymax"));
		int statuscode = 0; //Integer.parseInt(request.getParameter("statuscode"));
		String taskdesc = new String(request.getParameter("taskdesc").getBytes("iso8859_1"), "utf-8");
		String valval = request.getParameter("isignored"); 
		int isignored = (null == request.getParameter("isignored") ? 0 : 1);
		String[] depTaskIds = request.getParameterValues("depTaskIds");
		String[] subTaskIds = request.getParameterValues("subTaskIds");
		
		msg = "获得数据库连接";
		Connection conn = DBConnectionFactory.getCoreConnection();
		try
		{
			PreparedStatement ps = null; /*conn.prepareStatement("select * from taskschedule where datekey=? and taskid=?");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ResultSet rs = ps.executeQuery();
				try { if (rs.next()) throw new java.sql.SQLException("任务已经存在");
				} finally { rs.close(); }
			} finally { ps.close();	} */
			
			// clean old rec
			msg = "清理任务依赖关系";
			ps = conn.prepareStatement("delete from taskdependence where datekey=? and (taskid=? or deptaskid = ?)");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ps.setInt(3, taskid);
			ps.executeUpdate();
			} finally { ps.close();	}
	
			msg = "清理任务计划";
			ps = conn.prepareStatement("delete from taskschedule where datekey=? and taskid=?");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ps.executeUpdate();
			} finally { ps.close();	}
			
			// insert new rec
			msg = "更新任务计划";
			ps = conn.prepareStatement("insert into taskschedule (datekey, taskid, execurl, retrymax, statuscode, taskdesc, isignored) values (?,?,?,?,?,?,?)");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ps.setString(3, execurl);
			ps.setInt(4, retrymax);
			ps.setInt(5, statuscode);
			ps.setString(6, taskdesc);
			ps.setInt(7, isignored);
			ps.executeUpdate();
			} finally { ps.close();	}
			
			msg = "更新任务依赖关系";
			if (null != depTaskIds || null != subTaskIds)
			{
				ps = conn.prepareStatement("insert into taskdependence (datekey, taskid, deptaskid) values (?,?,?)");
				try {
				for (int i = 0; null != depTaskIds && i < depTaskIds.length; ++i)
				{
					ps.setDate(1, datekey);
					ps.setInt(2, taskid);
					ps.setString(3, depTaskIds[i]);
					ps.addBatch();
				}
				for (int i = 0; null != subTaskIds && i < subTaskIds.length; ++i)
				{
					ps.setDate(1, datekey);
					ps.setString(2, subTaskIds[i]);
					ps.setInt(3, taskid);
					ps.addBatch();
				}
				ps.executeBatch();
				} finally { ps.close(); }
			}
%>
	<result>0</result>
	<message>计划任务信息更新成功</message>
	<remark></remark>
<%
		}
		finally
		{
			conn.close();
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
%>
	<result>-1</result>
	<message><%=msg %>失败</message>
	<reason><![CDATA[<%= e.getMessage()%>]]</reason>
	<remark><![CDATA[<% e.printStackTrace(new java.io.PrintWriter(out)); %>]]></remark>
<%
	}
%>
</handleTaskUpdate>
